package zhieasy.cn.xpu_helper.controller.initiation;

import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import zhieasy.cn.common.utils.result.ResultUtils;
import zhieasy.cn.common.vo.ResultVO;
import zhieasy.cn.model.common.WechatUser;
import zhieasy.cn.model.initiation.AdminCollege;
import zhieasy.cn.service.initiation.AdminCollegeService;
import zhieasy.cn.service.initiation.StudentService;
import zhieasy.cn.service.initiation.TeacherService;

import java.util.Objects;

/**
 * @author sofency
 * @date 2020/8/2 14:14
 * @package IntelliJ IDEA
 * @description
 */
@RestController
@RequestMapping("/initiation")
public class AdminCollegeController {
//
    @Autowired
    AdminCollegeService adminCollegeService;

    @Autowired
    TeacherService teacherService;

    @Autowired
    StudentService studentService;

    //只允许辅导员登录
//    @RolesAllowed("5")
    @ApiOperation(value = "登录",notes = "登录")
    @RequestMapping(value = "/admin",method = RequestMethod.GET)//第一次登录
    public ResultVO loginByAdmin(){
        String openId = WechatUser.getWechatUserOpenIdFromSecurityContext();
        AdminCollege adminCollege = adminCollegeService.loginByManager(openId);
        if(Objects.isNull(adminCollege)){
            return ResultUtils.error();
        }else{
            return ResultUtils.success(adminCollege);
        }
    }

    @ApiOperation(value = "申请权限",notes = "申请权限")
    @RequestMapping(value = "/apply",method = RequestMethod.GET)
    public ResultVO applyCode(String inviteCode){
        Boolean aBoolean = teacherService.loginByCode(inviteCode);
        return aBoolean?ResultUtils.success():ResultUtils.error();
    }


    @ApiOperation("查询是否有用户的信息")
    @RequestMapping(value = "/login",method = RequestMethod.GET)
    public ResultVO login(){
        String openId = WechatUser.getWechatUserOpenIdFromSecurityContext();
        Boolean flag  = studentService.isHaveInfo(openId);
        if (flag)
            return ResultUtils.success("该学生注册过");
        else
            return ResultUtils.error("该学生未注册");
    }

    @ApiOperation("注册个人信息")
    @RequestMapping(value = "/register",method = RequestMethod.GET)
    public ResultVO register(@ApiParam("学生的唯一的id") String id ){
        Boolean register = studentService.register(id);
        return register?ResultUtils.success():ResultUtils.error();
    }
}
